﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resehantering.ServiceReference1;

namespace Resehantering
{
public partial class About : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            //bensin
            TextBox1.Enabled = false;
            if (!IsPostBack)
            {   
                getResor();
                getReseforskott();
            }
            
        }

        //räknar ut antal dagar man kan ha semester
        protected void Semesterdagar(object sender, EventArgs e)
        {
            DateTime datum1 = Calendar1.SelectedDate.Date; //datumobjekt 1
            DateTime datum2 = Calendar2.SelectedDate.Date; //datumobjekt 2
            int antal = (int)(datum2 - datum1).TotalDays;
            if (antal > 0)
            {
                DropDownList1.Items.Clear(); //dropdownList för semesterdagar
                ddlFrukost.Items.Clear(); //dropdownList för frukost
                ddlLunch.Items.Clear(); //dropdownList för lunch
                ddlMiddag.Items.Clear(); //dropdownList för middag

                for (int i = 0; i <= antal; i++)
                {
                    DropDownList1.Items.Add(i.ToString());
                    ddlFrukost.Items.Add(i.ToString());
                    ddlLunch.Items.Add(i.ToString());
                    ddlMiddag.Items.Add(i.ToString());
                }
            }
            else
            {
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("0");
                
                ddlFrukost.Items.Clear();
                ddlFrukost.Items.Add("0");

                ddlLunch.Items.Clear();
                ddlLunch.Items.Add("0");

                ddlMiddag.Items.Clear();
                ddlMiddag.Items.Add("0");
            }
        }

        protected void spara_Click(object sender, EventArgs e)
        {
            if (RadioButton1.Checked == true || RadioButton2.Checked == true && TextBox2.Text != "" && TextBox3.Text != "" && TextBox4.Text != "")
            {
                string fardsatt = "0";
                if (RadioButton1.Checked == true) fardsatt = TextBox1.Text;

                string vara = TextBox2.Text;
                string valuta = "Testvaluta"; //Fixa textfält!
                double pris = Double.Parse(TextBox3.Text);
                double moms = Double.Parse(TextBox4.Text);
                bool kvitto = false;
                if (CheckBox2.Checked) kvitto = true;

                Utlagg utlagg = new Utlagg(1, vara, valuta, pris, moms, kvitto, 1);

                UtlaggLista utlaggLista = new UtlaggLista();
                utlaggLista.addToLista(utlagg);
                updateUtlagg();

                RadioButton1.Checked = false;
                RadioButton2.Checked = false;
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
                CheckBox2.Checked = false;
            }
            else
            {

            }
            
        }

        //utlägg-bil
        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            TextBox1.Enabled = true;
        }

        //utlägg-annat
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            TextBox1.Enabled = false;
        }

        protected void updateUtlagg() 
        {
            
            ListBox1.Items.Clear();                                             //Tar bort alla befintliga i listbox

            UtlaggLista utlaggLista = new UtlaggLista();
            List<Utlagg> everyUtlagg = utlaggLista.getLista();
            if(everyUtlagg != null)
            {
                foreach (Utlagg utlagg in everyUtlagg)
                {
                    string text = utlagg.Typ + " " + utlagg.Summa;
                    ListItem item = new ListItem(text, utlagg.UtlaggID.ToString()); //Sparar tillsammans med ID för att veta vilket
                    ListBox1.Items.Add(item);                                       //item som är vilket utlägg
                }
            }

        }

        //Metod som returnerar det högsta nuvarande Id-numret i rese-tabellen
        protected int getHighestResaId() 
        {
            int highestId = 0;

            ServiceReference1.Service1Client wcfClient = new ServiceReference1.Service1Client();
            List<Resa> resor = wcfClient.GetResor();

            foreach (Resa resa in resor) 
            {
                if (resa.ResaID > highestId)
                    highestId = resa.ResaID;
            }

            return highestId;
        }

        protected void btn_rapportera_Click(object sender, EventArgs e)
        {
            //################### Hämta ut värden ###################
            string name = "";
            if(User.Identity.Name.Equals(""))
            {
                name = "John Doe";
            }
            else
            {
                name = User.Identity.Name;
            }

            int semesterdagar = Convert.ToInt32(DropDownList1.SelectedValue);
            
            string land = ddlCountry.SelectedValue;
            int antalFrukost = Int32.Parse(ddlFrukost.SelectedItem.Text);
            int antalLunch = Int32.Parse(ddlLunch.SelectedItem.Text);
            int antalMiddag = Int32.Parse(ddlMiddag.SelectedItem.Text);

            int totaltAvdrag = (antalFrukost * 44) + (antalLunch * 77) + (antalMiddag * 77);

            string inrikesUtrikes = "";
            if (land.ToLower().Equals("se"))
            {
                inrikesUtrikes = "inrikes";
            }
            else
            {
                inrikesUtrikes = "utrikes";
            }

            bool startresa = CheckBox1.Checked;
            string anledning = TextBox5.Text;
            
            DateTime datum1 = Calendar1.SelectedDate.Date; //datumobjekt 1
            DateTime datum2 = Calendar2.SelectedDate.Date; //datumobjekt 2

            bool godkand = false;
            
            //#######################################################

            int antal = (int)(datum2 - datum1).TotalDays;
            if (antal > 0 & anledning != "")// validering
            {
                ServiceReference1.Service1Client wcfClient = new ServiceReference1.Service1Client();
                Resa myResa = new Resa(1, name, semesterdagar, inrikesUtrikes, startresa, anledning, datum1, datum2, land, godkand,totaltAvdrag);
                
                List<Resa> resor = new List<Resa>();
                resor.Add(myResa);

                //Sparar resor
                wcfClient.SetResor(resor);

                //Ändrar reseförskott till utnyttjad
                if (ddlReseforskott.SelectedIndex > 0)
                {
                    short id = short.Parse(ddlReseforskott.SelectedItem.Value);
                    wcfClient.UpdateReseforskott(id, 1);
                }

                //Sparar utlägg
                UtlaggLista utlaggLista = new UtlaggLista();
                List<Utlagg> everyUtlagg = utlaggLista.getLista();
                if (everyUtlagg != null && everyUtlagg.Count > 0)
                {
                    wcfClient.SetUtlagg(everyUtlagg);
                }
                utlaggLista.emptyLista();
                getResor();
                getReseforskott();
                clearFields();
                updateUtlagg();
                Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'><SCRIPT language=\"JavaScript\">alert('Ny resa registrerad!');</SCRIPT>");
            }
            else
            {
                Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'><Script language='JavaScript'>alert('Fyll i alla fält!')</Script>");
            }
        }

        protected void getResor()
        {
            ServiceReference1.Service1Client wcfClient = new ServiceReference1.Service1Client();
            string userName = User.Identity.Name.ToLower();
            if(userName.Equals(""))
                userName = "John Doe";
            List<Resa> resor = wcfClient.GetResorByUser(userName);
            ListBoxResor.Items.Clear();
            foreach (Resa resa in resor)
            { 
                string text = resa.Land + " " + resa.StartDatum.ToShortDateString() + " - " + resa.SlutDatum.ToShortDateString();
                ListItem item = new ListItem(text, resa.ResaID.ToString());
                ListBoxResor.Items.Add(item);
            }
        }

        protected void getUtlagg(int id)
        {
            ServiceReference1.Service1Client wcfClient = new ServiceReference1.Service1Client();
            UtlaggLista utlaggLista = new UtlaggLista();
            utlaggLista.emptyLista();
            List<Utlagg> everyUtlagg = wcfClient.GetUtlaggByResa(id);
            foreach (Utlagg utlagg in everyUtlagg)
            {
                utlaggLista.addToLista(utlagg);
            }
            updateUtlagg();
        }

        protected void clearFields()
        {
            ddlCountry.SelectedIndex = 20;
            Calendar1.SelectedDate = DateTime.Now;
            Calendar2.SelectedDate = DateTime.Now + TimeSpan.FromDays(1);
            DropDownList1.SelectedIndex = 0;
            CheckBox1.Checked = false;
            TextBox5.Text = "";
            RadioButton2.Checked = true;
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            CheckBox2.Checked = false;
            ddlReseforskott.SelectedIndex = 0;
        }

        protected void disableFields()
        {
            ddlCountry.Enabled = false;
            Calendar1.Enabled = false;
            Calendar2.Enabled = false;
            DropDownList1.Enabled = false;
            CheckBox1.Enabled = false;
            TextBox5.Enabled = false;
            RadioButton2.Enabled = false;
            TextBox1.Enabled = false;
            TextBox2.Enabled = false;
            TextBox3.Enabled = false;
            TextBox4.Enabled = false;
            CheckBox2.Enabled = false;
            Button1.Enabled = false;
            ListBox1.Enabled = false;
            btn_rapportera.Enabled = false;
            btnSkrivUtRapport.Enabled = false;
            ddlReseforskott.Enabled = false;
        }

        protected void enableFields()
        {
            ddlCountry.Enabled = true;
            Calendar1.Enabled = true;
            Calendar2.Enabled = true;
            DropDownList1.Enabled = true;
            CheckBox1.Enabled = true;
            TextBox5.Enabled = true;
            RadioButton2.Enabled = true;
            TextBox1.Enabled = true;
            TextBox2.Enabled = true;
            TextBox3.Enabled = true;
            TextBox4.Enabled = true;
            CheckBox2.Enabled = true;
            Button1.Enabled = true;
            ListBox1.Enabled = true;
            btn_rapportera.Enabled = true;
            btnSkrivUtRapport.Enabled = true;
            ddlReseforskott.Enabled = true;
        }

        protected void btn_showResa_Click(object sender, EventArgs e)
        {
            ListItem item = ListBoxResor.SelectedItem;
            int resaID = Int32.Parse(item.Value);
            ServiceReference1.Service1Client wcfClient = new ServiceReference1.Service1Client();
            List<Resa> resor = wcfClient.GetResorByUserID(resaID);
            getUtlagg(resaID);
            Resa resa = resor[0];

            enableFields();
            ddlCountry.SelectedValue = resa.Land;
            Calendar1.SelectedDate = resa.StartDatum;
            Calendar2.SelectedDate = resa.SlutDatum;
            //DropDownList1.SelectedIndex = resa.SemesterDagar;
            CheckBox1.Checked = resa.SverigeStart;
            TextBox5.Text = resa.Anledning;
            RadioButton2.Checked = false;
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Enabled = false;
            TextBox4.Enabled = false;
            CheckBox2.Enabled = false;
            disableFields();
        }

        protected void btn_Rensa_Click(object sender, EventArgs e)
        {
            enableFields();
            clearFields();
        }

        protected void getReseforskott()
        {
            ServiceReference1.Service1Client wcfClient = new ServiceReference1.Service1Client();
            string userName = User.Identity.Name.ToLower();
            if (userName.Equals(""))
                userName = "John Doe";

            List<ReseForskott> reseforskott = wcfClient.GetReseForskottByUser(userName);

            ddlReseforskott.Items.Clear();
            
            if(reseforskott.Count == 0)
                ddlReseforskott.Items.Add(new ListItem("Du har inga reseförskott", "0"));
            else
                ddlReseforskott.Items.Add(new ListItem("Välj ett reseförskott i listan","0"));

            foreach (ReseForskott forskott in reseforskott)
            { 
                if(!forskott.IsUtnyttjad)
                {
                    String text = forskott.ReseForskottID + ". " + forskott.Belopp + " kr";
                    ListItem item = new ListItem(text, forskott.ReseForskottID.ToString());
                    ddlReseforskott.Items.Add(item);
                }
            }
        }
    
        protected void skrivUtRapport_Click(object sender, EventArgs e)
        {
            Response.Redirect("/skriv_ut.aspx");
        }
    }
}
